#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>

#define CHRDEVBASE_MAJOR 200
#define CHRDEVBASE_NAME  "chrdevbase"

static char readbuf[100];
static char writebuf[100];
static char kerneldata[] = {"kernel data"};

static int chrdevbase_open(struct inode *inode, struct file *flip)
{
	return 0;
}

static ssize_t chrdevbase_read(struct file *flip, char __user *buf, size_t cnt, loff_t *off_t)
{
	int ret =0;

	memcpy(readbuf,kerneldata,sizeof(kerneldata));
	ret = copy_to_user(buf, readbuf, cnt);
	if(ret ==0)
	{
		printk("kernel senddata ok\r\n");
	}
	else
	{
		printk("kernel senddata failed\r\n");
	}

	return 0;
}

static ssize_t chrdevbase_write(struct file *flip, const char __user *buf, size_t cnt, loff_t *off_t)
{
	int ret=0;

	ret = copy_from_user(writebuf, buf, cnt);
	if(ret ==0)
	{
		printk("kernel recvdata ok\r\n");
	}
	else
	{
		printk("kernel recvdata failed\r\n");
	}

	return 0;
}

static int chrdevbase_release(struct inode *inode, struct file *flip)
{
	return 0;
}

static struct file_operations chrdevbase_fops = 
{
	.owner 		= THIS_MODULE,
	.open 		= chrdevbase_open,
	.read 		= chrdevbase_read,
	.write 		= chrdevbase_write,
	.release 	= chrdevbase_release,
};

static int __init chrdevbase_init(void)
{
	int ret =0;

	ret = register_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME, &chrdevbase_fops);
	if(ret <0)
	{
		printk("chrdevbase init failed\r\n");
	}

	printk("chrdevbase init sucess\r\n");
	return 0;
}

static void __exit chrdevbase_exit(void)
{
	unregister_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME);

	printk("chrdevbase exit sucess\r\n");
}

module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("jtb");

